﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;

using AddProductLib.UserTextBox;
using SkladLib;
using MessageBox = System.Windows.MessageBox;


namespace Rezervace
{
    public partial class AddProduct : Window
    {
        #region Proměnné + metody

        private DateTime _odDne = DateTime.Now;

        private Zbozi _zbozi;

        // spocitat rozdil mezi 2 datumy
        private static int PocetDnu(DateTime od, DateTime doKdy)
        {
            TimeSpan rozdil = doKdy.Date.Subtract(od.Date);
            return rozdil.Days;
        }

        private void VynulovatHodnoty()
        {
            date_OdDne.SelectedDate = DateTime.Now;

            _zbozi = null;

            tb_CarovyKod.Text = "";
            tb_Nazev.Text = "";
            tb_PocetDni.Text = "1";

            lb_StatusZbozi.Visibility = Visibility.Hidden;
            lb_Stav.Visibility = Visibility.Hidden;
        }

        private void UpdateStatus()
        {
            if (_zbozi == null)
            {
                lb_StatusZbozi.Visibility = Visibility.Hidden;
                lb_Stav.Visibility = Visibility.Hidden;
                MujExpander.Visibility = Visibility.Hidden;
            }

            else
            {
                List<SkladLib.Rezervace> listRezervace = _zbozi.rezervace;

                lb_StatusZbozi.Visibility = Visibility.Visible;
                lb_Stav.Visibility = Visibility.Visible;

                if (listRezervace.Count == 0)
                {
                    lb_StatusZbozi.Content = "Dostupný";
                    lb_StatusZbozi.Foreground = Brushes.DarkGreen;

                    _odDne = DateTime.Now;
                    date_OdDne.SelectedDate = _odDne;

                    MujExpander.Visibility = Visibility.Hidden;
                }
                else
                {
                    MujExpander.Visibility = Visibility.Visible;

                    list_Prehled.Items.Clear();

                    foreach (SkladLib.Rezervace rezervace in listRezervace)
                    {
                        list_Prehled.Items.Add(new RezervaceZakaznik("Jméno: " + rezervace.Name,
                                "Od:    " + rezervace.DateFrom.ToShortDateString(),
                                "Do:    " + rezervace.DateIn.ToShortDateString()));
                        _odDne = rezervace.DateIn.AddDays(1);
                    }

                    lb_StatusZbozi.Content = "Rezervovaný";
                    lb_StatusZbozi.Foreground = Brushes.Red;

                    date_OdDne.SelectedDate = _odDne;
                }

            }
        }

        #endregion

        #region Form

        public AddProduct()
        {
            InitializeComponent();

            tb_CarovyKod.Focus();
        }

        public AddProduct(string carovyKod)
        {
            InitializeComponent();

            _zbozi = Zbozi.GetZboziByCode(carovyKod);

            try
            {
                tb_CarovyKod.Text = carovyKod;
                tb_Nazev.Text = _zbozi.name;

                tb_CarovyKod.Focus();
            }
            catch (Exception)
            {
                MessageBox.Show("Produkt není zaznamenán v databázi", "Upozornění", MessageBoxButton.OK, MessageBoxImage.Warning);
                this.Close();
            }
        }


        // konstruktor s parametry - pro možnost Upravit
        public AddProduct(NovyZbozi novy)
        {
            InitializeComponent();

            if (novy.CarovyKod != string.Empty)
            {
                _zbozi = Zbozi.GetZboziByCode(novy.CarovyKod);
                tb_CarovyKod.Text = novy.CarovyKod;
            }

            else
            {
                _zbozi = null;
                tb_CarovyKod.Text = "neznámý";
            }

            tb_Nazev.Text = novy.Nazev;
            date_OdDne.SelectedDate = novy.OdDne;
            _odDne = novy.OdDne; ;
            tb_PocetDni.Text = novy.PocetDni;

            tb_CarovyKod.Focus();
        }

        private void Okno_Loaded(object sender, RoutedEventArgs e)
        {
            date_OdDne.SelectedDateChanged += date_OdDne_SelectedDateChanged;

            if (tb_PocetDni.Text == string.Empty) tb_PocetDni.Text = "1";

            string[] nazvy = new Zbozi().GetAllZboziNames();
            foreach (string s in nazvy) tb_Nazev.AddItem(new AutoCompleteEntry(s, null));

            UpdateStatus();
        }

        private void Okno_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (_zbozi == null) return;

            e.Cancel = MessageBoxResult.Yes !=
                       MessageBox.Show("Opravdu chcete zrušit všechny změny", "Upozornění", MessageBoxButton.YesNo,
                                       MessageBoxImage.Warning);
        }

        #endregion

        #region Události komponentů


        private void tb_CarovyKod_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            switch (e.Text)
            {
                case "-":
                    InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new CultureInfo("en-US"));
                    e.Handled = true;
                    break;
                case "(":
                    InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new CultureInfo("cs-CZ"));
                    e.Handled = true;
                    break;
            }
        }

        private void tb_CarovyKod_GotFocus(object sender, RoutedEventArgs e)
        {
            if (_zbozi == null) tb_CarovyKod.Text = "";
        }

        private void tb_CarovyKod_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                _zbozi = Zbozi.GetZboziByCode(tb_CarovyKod.Text);
                tb_Nazev.Text = _zbozi.name;
            }
            catch (Exception)
            {
                if (tb_CarovyKod.Text.Length <= 0) return;

                MessageBox.Show("Zadali jste neznámý čarový kód!\n\nZboží není rozpoznáno !!", "Chyba", MessageBoxButton.OK, MessageBoxImage.Warning);
                VynulovatHodnoty();
            }
            finally
            {
                UpdateStatus();
            }
        }
        






        private void tb_Nazev_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                _zbozi = Zbozi.GetZboziByName(tb_Nazev.Text);
                tb_CarovyKod.Text = _zbozi.code;
            }
            catch (Exception)
            {
                VynulovatHodnoty();
            }
            finally
            {
                UpdateStatus();
            }

        }





        private void tb_PocetDni_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {
            int result;
            if (!Int32.TryParse(e.Text, out result)) e.Handled = true;
        }

        private void tb_PocetDni_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (tb_PocetDni.Text.Length > 0 && date_OdDne.SelectedDate != null)

                MujKalendar.SelectedDate = MujKalendar.DisplayDate =
                                        date_OdDne.SelectedDate.Value.AddDays(int.Parse(tb_PocetDni.Text));
            else if (date_OdDne.SelectedDate != null)
                MujKalendar.SelectedDate = MujKalendar.DisplayDate = date_OdDne.SelectedDate.Value.AddDays(1);
        }

        private void tb_PocetDni_LostFocus(object sender, RoutedEventArgs e)
        {
            if (tb_PocetDni.Text == string.Empty) tb_PocetDni.Text = "1";
        }





        private void date_OdDne_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            int pocet = PocetDnu(_odDne, (DateTime)date_OdDne.SelectedDate);
            if (pocet < 0)
            {
                MessageBox.Show("Neplatné datum!\nZvolte znovu!", "Upozornění", MessageBoxButton.OK,
                                MessageBoxImage.Warning);
                date_OdDne.SelectedDate = _odDne;
            }

            else
            {
                // resetuje moznosti
                tb_PocetDni.Text = "1";
                MujKalendar.SelectedDate = MujKalendar.DisplayDate = date_OdDne.SelectedDate.Value.AddDays(1);
            }
        }

        private void MujKalendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            if (date_OdDne.SelectedDate == null) return;

            int pocet = PocetDnu((DateTime)date_OdDne.SelectedDate, (DateTime)MujKalendar.SelectedDate);
            if (pocet <= 0)
            {
                MessageBox.Show("Neplatné datum!\nZvolte znovu!", "Upozornění", MessageBoxButton.OK, MessageBoxImage.Warning);
                tb_PocetDni.Text = "1";
                MujKalendar.SelectedDate = MujKalendar.DisplayDate = date_OdDne.SelectedDate.Value.AddDays(1);
            }

            else tb_PocetDni.Text = pocet.ToString();

            lb_DatumVraceni.Content = MujKalendar.DisplayDate.ToString("dddd, dd MMMM, " + MujKalendar.DisplayDate.Year);
        }



        #endregion

        #region Potvrdit, Storno

        // delegát
        public delegate void PridatRezervaciHandler(object sender, PridatRezervaciEventArgs e);

        public event PridatRezervaciHandler PridatRezervaci;

        private void bt_Pridat_Click(object sender, RoutedEventArgs e)
        {
            if (_zbozi == null)
            {
                if (MessageBoxResult.Yes ==
                MessageBox.Show("Nebylo vybrané žádné zboží", "Upozornění", MessageBoxButton.OK,
                                MessageBoxImage.Warning))
                    this.Close();
            }
            else
            {
                PridatRezervaciEventArgs args = new PridatRezervaciEventArgs(tb_CarovyKod.Text, tb_Nazev.Text,
                                                                             (DateTime)date_OdDne.SelectedDate,
                                                                             tb_PocetDni.Text,
                                                                             (DateTime)MujKalendar.SelectedDate);
                PridatRezervaci(this, args);

                _zbozi = null;
                this.Close();
            }
        }

        private void bt_Storno_Click(object sender, RoutedEventArgs e)
        {
            if (_zbozi == null) return;

            if (MessageBoxResult.Yes ==
                MessageBox.Show("Opravdu chcete zrušit všechny změny", "Upozornění", MessageBoxButton.YesNo,
                                MessageBoxImage.Warning))
                this.Close();
        }

        #endregion


    }
}
